
--[[
    目前 复合型 热重载会泄露 还没有检查原因 
]]

load_fdf [[

    // 按钮
    Frame "TEXTBUTTON" "textbutton2" {
        Width 0.016,
        Height 0.016,
        //设置正常状态下背景
        ButtonPushedTextOffset -0.002f -0.003f, 

        ControlBackdrop "BattleNetButtonBackdropTemplate",
        Frame "BACKDROP" "BattleNetButtonBackdropTemplate" {
            BackdropTileBackground,
            BackdropBackground  "UI\Widgets\Glues\GlueScreen-Button1-BackdropBackground.blp",
            BackdropCornerFlags "UL|UR|BL|BR|T|L|B|R",
            BackdropCornerSize  0.016,
            BackdropBackgroundSize  0.032,
            BackdropBackgroundInsets 0.004 0.004 0.004 0.004,
            BackdropEdgeFile  "UI\Widgets\Glues\GlueScreen-Button1-BackdropBorder.blp",
        }
        
        //设置禁用状态下背景
        ControlDisabledBackdrop "BattleNetButtonDisabledBackdropTemplate",
        Frame "BACKDROP" "BattleNetButtonDisabledBackdropTemplate" {
            BackdropTileBackground,
            BackdropBackground  "UI\Widgets\Glues\GlueScreen-Button1-BackdropBackground-Disabled.blp",
            BackdropCornerFlags "UL|UR|BL|BR|T|L|B|R",
            BackdropCornerSize  0.016,
            BackdropBackgroundSize  0.032,
            BackdropBackgroundInsets 0.004 0.004 0.004 0.004,
            BackdropEdgeFile  "UI\Widgets\Glues\GlueScreen-Button1-BackdropBorder-Disabled.blp",
        }

        //设置按下状态下背景 
        ControlPushedBackdrop "BattleNetButtonPushedBackdropTemplate",
        Frame "BACKDROP" "BattleNetButtonPushedBackdropTemplate" {
            BackdropTileBackground,
            BackdropBackground  "UI\Widgets\Glues\GlueScreen-Button1-BackdropBackground.blp",
            BackdropCornerFlags "UL|UR|BL|BR|T|L|B|R",
            BackdropCornerSize  0.016,
            BackdropBackgroundSize  0.032,
            BackdropBackgroundInsets 0.004 0.004 0.004 0.004,
            BackdropEdgeFile  "UI\Widgets\Glues\GlueScreen-Button1-BackdropBorder-Down.blp",
        }
        //文本
        ButtonText "BattleNetButtonTextTemplate",
        Frame "TEXT" "BattleNetButtonTextTemplate" {
            FrameFont "MasterFont", 0.013, "",
            FontJustificationH JUSTIFYCENTER,
            FontJustificationV JUSTIFYMIDDLE,    
            FontJustificationOffset 0.0 -0.001,
            FontFlags "FIXEDSIZE",
            FontColor 0.99 0.827 0.0705 1.0,
            FontHighlightColor 1.0 1.0 1.0 1.0,
            FontDisabledColor 0.5 0.5 0.5 1.0,
            FontShadowColor 0.0 0.0 0.0 0.9,	
            FontShadowOffset 0.002 -0.002,	
        }

    }


    
    Frame "TEXTBUTTON" "textbutton" {
        Width 0.016,
        Height 0.016,
        ButtonPushedTextOffset 0.002f -0.002f,    

        ControlBackdrop "ButtonBackdropTemplate2",
        Frame "BACKDROP" "ButtonBackdropTemplate2" {
            DecorateFileNames,
            BackdropTileBackground,
            BackdropBackground  "EscMenuButtonBackground",
            BackdropCornerFlags "UL|UR|BL|BR|T|L|B|R",
            BackdropCornerSize  0.0125,
            BackdropBackgroundSize  0.256,
            BackdropBackgroundInsets 0.005 0.005 0.005 0.005,
            BackdropEdgeFile  "EscMenuButtonBorder",
        }

        ControlDisabledBackdrop "ButtonDisabledBackdropTemplate2",
        Frame "BACKDROP" "ButtonDisabledBackdropTemplate2" {
            DecorateFileNames,
            BackdropTileBackground,
            BackdropBackground  "EscMenuButtonDisabledBackground",
            BackdropCornerFlags "UL|UR|BL|BR|T|L|B|R",
            BackdropCornerSize  0.0125,
            BackdropBackgroundSize  0.256,
            BackdropBackgroundInsets 0.005 0.005 0.005 0.005,
            BackdropEdgeFile  "EscMenuButtonDisabledBorder",
        }

        ControlPushedBackdrop "ButtonPushedBackdropTemplate2",
        Frame "BACKDROP" "ButtonPushedBackdropTemplate2" {
            DecorateFileNames,
            BackdropTileBackground,
            BackdropBackground  "EscMenuButtonPushedBackground",
            BackdropCornerFlags "UL|UR|BL|BR|T|L|B|R",
            BackdropCornerSize  0.0125,
            BackdropBackgroundSize  0.256,
            BackdropBackgroundInsets 0.005 0.005 0.005 0.005,
            BackdropEdgeFile  "EscMenuButtonPushedBorder",
        }
        
        ButtonText "EscMenuButtonTextTemplate2",
        Frame "TEXT" "EscMenuButtonTextTemplate2" {
            FrameFont "MasterFont", 0.013, "",
            FontJustificationH JUSTIFYCENTER,
            FontJustificationV JUSTIFYMIDDLE,    
            FontJustificationOffset 0.0 -0.002,
            FontFlags "FIXEDSIZE",
            FontColor 0.99 0.827 0.0705 1.0,
            FontHighlightColor 1.0 1.0 1.0 1.0,
            FontDisabledColor 0.5 0.5 0.5 1.0,
            FontShadowColor 0.0 0.0 0.0 0.9,	
            FontShadowOffset 0.002 -0.002,	
        }
    }
    
]]

--- 文本按钮
local japi = require 'jass.japi'

local textbutton = {}
setmetatable(textbutton,textbutton)

textbutton.alltextbuttons = {}

--- 继承按钮
local mt = extends(class.button){}

--- 类型 
mt.type = 'textbutton'

--- fdf类型 
mt._type = 'TEXTBUTTON'

--(= FDF的名字)
mt.fdf = 'textbutton'

--- 按钮上的文字
mt.text = "文本按钮"

--字体
mt.font = "MasterFont"

--字体大小
mt.font_size = 12

--文本间距
mt.spacing = 0

--文本颜色
mt.color = 0xffffffff

--- 创建控件
function mt.new(parent, text, x, y, w, h)
    return class.textbutton:builder
    {
        parent = parent,
        text = text,
        w = w,
        h = h,
        x = x,
        y = y,
    }
end 

--- 输出
function mt:__tostring()
    return string.format('[文本按钮]->%d',self.handle or 0)
end

--- 初始化
function mt:init()
    class.button.init(self)
    if package.local_test then 
        local info = debug.getinfo(5, "Sl") or {}
        local bool = info.short_src:find('core')
        --- 不是底层创建的控件记录路径用来热更新
        if not bool then 
            self._src = info.short_src
            self._line = info.currentline
        end 
    end 
    local childs = japi.FrameGetChildlist(self.handle)
    self.child4 = class.text.convert(childs[4], self)
    self:set_font(self.font,self.font_size)
    self:set_spacing(self.spacing)
    self:set_color(self.color)
    self:set_text(self.text)
    textbutton.alltextbuttons[self.handle] = self 
    return self
end

--- 删除
function mt:remove()
    if self.removed then return end 
    textbutton.alltextbuttons[self.handle] = nil
    self.child4:remove()
    class.button.remove(self)
end 

--- 设置按钮文字内容
--- @params name text
function mt:set_text(text)
    if self.removed then return end
    self.child4:set_text(text)
end 

--- 获取按钮文字内容
--- @return string
function mt:get_text()
    return self.child4:get_text()
end

--- 设置文本字体和大小
function mt:set_font(font_path,font_size)
    if self.removed then return end
    self.child4:set_font(font_path,font_size)
end

--- 设置文本间距 
function mt:set_spacing(spacing)
    if self.removed then return end
    self.child4:set_spacing(spacing)
end

--- 设置颜色
function mt:set_color(color)
    if self.removed then return end
    self.child4:set_color(color)
end

--- 获取颜色
function mt:get_color()
    return self.child4:get_color()
end 

class.textbutton = mt

return mt 